{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Web"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: web.py in d:\\programdata\\miniconda3\\lib\\site-packages (0.62)\n",
      "Requirement already satisfied: cheroot in d:\\programdata\\miniconda3\\lib\\site-packages (from web.py) (8.5.2)\n",
      "Requirement already satisfied: jaraco.functools in d:\\programdata\\miniconda3\\lib\\site-packages (from cheroot->web.py) (3.5.0)\n",
      "Requirement already satisfied: six>=1.11.0 in d:\\programdata\\miniconda3\\lib\\site-packages (from cheroot->web.py) (1.16.0)\n",
      "Requirement already satisfied: more-itertools>=2.6 in d:\\programdata\\miniconda3\\lib\\site-packages (from cheroot->web.py) (8.12.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Did not find path entry D:\\ProgramData\\Miniconda3\\bin\n"
     ]
    }
   ],
   "source": [
    "! pip install web.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的输出被隐藏了，点击 \"+\" 号可以展开。查看官方文档学习[如何隐藏单元格输出](https://jupyterbook.org/interactive/hiding.html)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%%writefile` 是 Jupyter Notebook 的一个[魔法命令](https://ipython.readthedocs.io/en/stable/interactive/magics.html)，表示将下面的内容写入到 app.py 文件中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting app.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile app.py\n",
    "import web\n",
    "\n",
    "urls = ( '/', 'index' )\n",
    "app = web.application(urls, globals())\n",
    "\n",
    "class index:\n",
    "    def GET(self):\n",
    "        greeting = \"Hello World\"\n",
    "        return greeting\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%run` 也是 Jupyter Notebook 的魔法命令，它将会执行 `app.py` 文件（使用 8081 端口）。\n",
    "但是，下面这一行代码一旦运行就不会停止，因此上传代码到 Readthedocs 后就会在编译时卡住，这里就给它先注释掉了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %run app.py 8081"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "使用这段程序时，应当解开上面一行代码的注释，然后访问 <http://localhost:8081/>，就可以看到 Hello world 被成功打印了。"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
